<?php
include_once 'IndexPresenter.php';
include_once 'AbstractView.php';
include_once 'UserInfoPresenter.php';
include_once 'InsultBuilder.php';
class UserProfileView extends AbstractView {
	private $presenter;
	private $invokedUserName;
	public function __construct($userName) {
		parent::__construct ();
		$this->presenter = new UserInfoPresenter ( $this, parent::getUserRepository (), $userName );
		$this->invokedUserName = $userName;
	}
	public function userUpdateSucceed() {
		$builder = new InsultBuilder ();
		$builder->setMessage ( "You did it! Wait... YOU DID IT!!!!!!!!! AHAHAHAHAHA!!!!!1" )->setTitle ( "Mkay" )->setPicture ( "update_succeed.jpg" )->setSource ( "userprofile.php?user=" . $this->invokedUserName );
		parent::redirect ( $builder->build () );
	}
	public function userUpdateError($reason) {
		$builder = new InsultBuilder ();
		$builder->setMessage ( $reason )->setTitle ( "No way" )->setPicture ( "update_failed.jpg" )->setSource ( "userprofile.php?user=" . $this->invokedUserName );
		parent::redirect ( $builder->build () );
	}
	public function render() {
		if (! parent::invokedByUser () && ! parent::invokedByAdmin ()) {
			parent::block ();
			return;
		}
		
		if ($this->presenter->getUserName () == NULL || $this->presenter->getUser ()->getUserName () !== parent::getActiveUser ()->getUserName ()) {
			$builder = new InsultBuilder ();
			$builder->setMessage ( "Only steers and queers plays with URL, and you don't much look like a steer to me so that kinda narrows it down!" )->setTitle ( "Like playing with URL?" )->setPicture ( "userprofile_user_does_not_exist.jpg" )->setSource ( "index.php" );
			parent::redirect ( $builder->build () );
			return;
		}
		
		if (isset ( $_POST ["userName"] )) {
			
			$this->presenter->updateUser ( trim ( $_POST ["userName"] ), trim ( $_POST ["name"] ), trim ( $_POST ["email"] ), trim ( $_POST ["currentPassword"] ), trim ( $_POST ["newPassword"] ), trim ( $_POST ["newPasswordAgain"] ) );
			return;
		}
		
		require 'userinfo.phtml';
	}
}

$view = new UserProfileView ( htmlspecialchars_decode ( $_GET ["user"] ) );
$view->render ();

?>